/**
 * 此文件用于会议页数据管理
 */
import { addFile, deleteFile, cfFileList, reserveConference, getToDoMeetingList, cfDetail, getHistCfList, getHistItem, deleteCf, editAgendaCf, getCfRecord, setCfRecord } from '@/api/conference'
import tools from '@/utils/tools'
import { ifContactInList } from '@/utils/socketTools'
import moment from 'moment'

const joinCfData = { // 加入会议专用
  cf_code: '', // 会议号
  code_error_msg: '', // 会议号下方错误提示
  voiced: false, // 是否打开麦克风
  camera: false, // 是否打开摄像头
  password_error_msg: '' // 密码框下方错误提示
}

const state = {
  rightHeaderData: {}, // 会议页右侧头部内容
  current_navId: '', // 会议页当前二级导航
  cfFileList: [], // 会议文档
  joinCfData: JSON.parse(JSON.stringify(joinCfData)),
  cfDrawerData: {}, // 会议详情弹框数据
  agendaCfData: { // 待办会议专用
    agendaCfList: [], // 待办会议列表
    editData: null, // 编辑页数据
    drawerId: '' // 展示详情的会议id
  },
  reserveCfData: { // 预定会议专用
    selectedList: [] // 表单已选参会人列表
  },
  historyCfDate: { // 历史会议专用
    historyCfList: [], // 历史会议列表
    total: 0 // 总数
  },
  // 历史会议选中会议的会议纪要
  cfRecord: ''
}

const mutations = {
  // 设置rightHeader
  set_rightHeader (state, obj) {
    state.rightHeaderData = obj
  },
  // 设置会议模块二级导航高亮
  set_currentId (state, id) {
    state.current_navId = id
  },
  // 重制加入会议专用数据
  reset_joinCfData (state) {
    state.joinCfData = JSON.parse(JSON.stringify(joinCfData))
  },
  // 设置输入的会议号
  set_joinCfData_cf_code (state, code) {
    state.joinCfData.cf_code = code
  },
  // 设置会议号下方错误提示
  set_joinCfData_code_error_msg (state, msg) {
    state.joinCfData.code_error_msg = msg
  },
  // 设置加入会议是否打开麦克风
  set_joinCfData_voiced (state, boolean) {
    state.joinCfData.voiced = boolean
  },
  // 设置加入会议是否打开摄像头
  set_joinCfData_camera (state, boolean) {
    state.joinCfData.camera = boolean
  },
  // 设置输入密码下方错误提示
  set_joinCfData_password_error_msg (state, msg) {
    state.joinCfData.password_error_msg = msg
  },
  // 设置预定会议文档 type:设置set 添加add（需要原列表）删除delete
  set_cfFileList (state, { list, type }) {
    if (type === 'set') {
      state.cfFileList = list
    } else if (type === 'add') { // 'add'添加
      const fileList = JSON.parse(JSON.stringify(state.cfFileList))
      list.forEach(item => {
        if (state.cfFileList.every(it => it.file_id !== item.file_id)) {
          fileList.unshift(item)
        }
      })
      state.cfFileList = fileList
    } else {
      // delete
      state.cfFileList = state.cfFileList.filter(item => item.file_id !== list[0])
    }
  },
  // 设置预定会议表单处参会人列表
  set_resCf_selectedList (state, { list, type }) {
    if (type === 'set') {
      state.reserveCfData.selectedList = list
    } else if (type === 'delete') {
      state.reserveCfData.selectedList = state.reserveCfData.selectedList.filter(item => item.user_id !== list.id)
    } else {
      const selectList = JSON.parse(JSON.stringify(state.reserveCfData.selectedList))
      list.forEach(item => {
        if (state.reserveCfData.selectedList.every(it => it.user_id !== item.user_id)) {
          selectList.push(item)
        }
      })
      state.reserveCfData.selectedList = selectList
    }
  },
  // 待办会议
  // 设置列表
  set_agendaCfList (state, list) {
    state.agendaCfData.agendaCfList = list
  },
  set_agendaEditData (state, data) {
    state.agendaCfData.editData = data
  },
  set_drawerId (state, cfId) {
    state.agendaCfData.drawerId = cfId
  },
  // 历史会议
  // 设置列表
  set_historyCfList (state, list) {
    state.historyCfDate.historyCfList = list
  },
  // 设置总数
  set_historyList_total (state, total) {
    state.historyCfDate.total = total
  },
  // 设置会议详情弹框数据
  set_cfDrawerData (state, data) {
    state.cfDrawerData = data
  },
  // 设置历史会议当前会议的会议纪要
  set_cfRecord (state, record) {
    state.cfRecord = record
  }
}

const actions = {
  // 获取待办会议列表
  getToDoMeetingList ({ commit }) {
    return new Promise((resolve, reject) => {
      const zone = -tools.time.getTimeZone()
      getToDoMeetingList(zone).then(res => {
        if (res.data.code === 200) {
          commit('set_agendaCfList', res.data.data)
        }
        resolve(res)
      })
    })
  },
  // 添加会议文档
  addCfFile ({ commit }, fileInfo) {
    return new Promise((resolve, reject) => {
      addFile(fileInfo).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 删除会议文档
  deleteCfFile ({ commit }, { cfID, fileID }) {
    return new Promise((resolve, reject) => {
      deleteFile(cfID, fileID).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取当前会议文件列表
  getCfFileList ({ commit }, cfID) {
    if (cfID) {
      return new Promise((resolve, reject) => {
        cfFileList(cfID).then(res => {
          if (res.data && res.data.code === 200) {
            if (!res.data.data) {
              res.data.data = []
            }
            res.data.data.forEach(item => {
              item.fileUploader = item.user_id
              delete item.user_id
            })
            commit('set_cfFileList', {
              list: res.data.data,
              type: 'set'
            })
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    } else {
      return false
    }
  },
  // 删除待办会议
  deleteCf (context, cfID) {
    return new Promise((resolve, reject) => {
      deleteCf(cfID).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 预定会议
  reserveCf ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      reserveConference(payload).then(res => {
        if (res.data.code === 200) {
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  // 更新预定会议/编辑待办会议
  editAgendaCf (context, payload) {
    return new Promise((resolve, reject) => {
      editAgendaCf(payload).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取会议详情
  getCfDetail (context, cfID) {
    return new Promise((resolve, reject) => {
      cfDetail(cfID).then(res => {
        if (res.data.code === 200 && res.data.data) {
          res.data.data.plan_participant.forEach(item => {
            ifContactInList(item, 1)
          })
          resolve(res)
        }
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 历史会议
  // 获取列表
  getHistoryCfList ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getHistCfList(payload).then(res => {
        if (res.data.code === 200 && res.data.data) {
          res.data.data.data.forEach(item => {
            item.cf_start_time = moment(+new Date(item.cf_start_time) - new Date().getTimezoneOffset() * 60000).format('YYYY-MM-DD HH:mm:ss')
          })
          commit('set_historyCfList', res.data.data.data)
          commit('set_historyList_total', res.data.data.total)
        }
      })
    })
  },
  // 获取历史会议详情
  getHistoryCfItem ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getHistItem(payload).then(res => {
        if (res.data.code === 200 && res.data.data) {
          res.data.data.cf_start_time = moment(+new Date(res.data.data.cf_start_time) - new Date().getTimezoneOffset() * 60000).format('YYYY-MM-DD HH:mm:ss')
          res.data.data.cf_end_time = moment(+new Date(res.data.data.cf_end_time) - new Date().getTimezoneOffset() * 60000).format('YYYY-MM-DD HH:mm:ss')
          res.data.data.cf_participants.forEach(async item => {
            await ifContactInList(item, 1)
          })
          commit('set_cfDrawerData', res.data.data)
        }
      })
    })
  },
  // 历史会议-查询会议纪要
  getCfRecord ({ commit }, cfID) {
    return new Promise((resolve, reject) => {
      getCfRecord(cfID).then(res => {
        if (res.data && res.data.code === 200) {
          commit('set_cfRecord', res.data.data)
        }
        resolve(res)
      }).catch(err => [
        reject(err)
      ])
    })
  },
  // 历史会议-设置会议纪要
  setCfRecord (context, { cfID, cfMinutes }) {
    return new Promise((resolve, reject) => {
      setCfRecord(cfID, cfMinutes).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
